package QRCG;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Util {
	/**
	 * @param s The string to be tested.
	 * @return Whether the given string isn't null and has some non-space text
	 *         content.
	 */
	public static boolean hasTextContent(String s) {
		return s != null && !s.trim().equals("");
	}

	/**
	 * @param s The for string which the MD5 hash will be generated.
	 * @return 32 Characters long MD5 hash of the given string.
	 */
	public static String getMD5Hash(String s) {
		try {
			// MD5 hashing the string s
			byte[] bytesOfMessage = s.getBytes();
			MessageDigest md = MessageDigest.getInstance("MD5");

			byte[] digest = md.digest(bytesOfMessage);
			BigInteger bigInt = new BigInteger(1, digest);
			String hashString = bigInt.toString(16);

			while (hashString.length() < 32) {
				hashString = "0" + hashString;
			}

			return hashString;

		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			// if there's an error while generating the hash, the original
			// string s will be returned
			return s;
		}
	}
}
